package concurrent;

import java.util.concurrent.*;

/**
 * 在一个线程中需要获取其他线程的执行结果，能想到几种方式？各有什么优缺点？
 *
 * @author jack.wu
 * @since 2019/12/24 16:53
 */
public class GetExecutionResultsDemo {

}

/**
 * 方式1：Thread的join()方法实现
 */
class Dome1 {

    /**
     * 用于封装结果
     */
    static class Result<T> {
        private T result;

        public T getResult() {
            return result;
        }

        public void setResult(T result) {
            this.result = result;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        Result<Integer> result = new Result<>();
        Thread thread = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                result.setResult(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        // 让主线程等待thread线程执行完毕之后再继续，join方法会让当前线程阻塞
        thread.join();
        Integer res = result.getResult();
        System.out.println(System.currentTimeMillis());
        System.out.println(res);
    }
}

/**
 * 方式2：CountDownLatch实现
 * 使用CountDownLatch可以让一个或者多个线程等待一批线程完成之后，自己再继续
 */
class Dome2 {

    public static void main(String[] args) throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        CountDownLatch downLatch = new CountDownLatch(1);
        // 用于存放子线程执行结果
        Dome1.Result<Integer> result = new Dome1.Result<>();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                result.setResult(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                downLatch.countDown();
            }
        }).start();
        downLatch.await();
        Integer res = result.getResult();
        System.out.println(System.currentTimeMillis());
        System.out.println(res);
    }
}

/**
 * 方式3：ExecutorService.submit方法实现
 */
class Dome3{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newCachedThreadPool();
        System.out.println(System.currentTimeMillis());
        Future<Integer> future = service.submit(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        });
        service.shutdown();
        System.out.println(System.currentTimeMillis());
        System.out.println(future.get());
    }
}

/**
 * 方式4：FutureTask方式1
 */
class Dome4{

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(System.currentTimeMillis());
        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
            TimeUnit.SECONDS.sleep(2);
            return 10;
        });
        new Thread(futureTask).start();
        System.out.println(System.currentTimeMillis());
        System.out.println(futureTask.get());
    }
}

/**
 * 方式5：FutureTask方式2
 */
class Dome5{

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(System.currentTimeMillis());
        FutureTask<Integer> futureTask = new FutureTask<>(() -> 10);
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            futureTask.run();
        }).start();
        System.out.println(System.currentTimeMillis());
        System.out.println(futureTask.get());
    }
}

/**
 * 方式6：CompletableFuture方式实现
 */
class Dome6{

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(System.currentTimeMillis());
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        });
        System.out.println(System.currentTimeMillis());
        System.out.println(future.get());
    }
}
